package com.box.app.arithmetic

internal class Variable1(private val symbol: String) : Argument {
    private fun tryParse(variables: Map<String, Number>?): Number? {
        return variables.getValue(symbol) ?: symbol.toNumberOrNull()
    }

    @Throws(ArithmeticError::class)
    fun parse(variables: Map<String, Number>?) = tryParse(variables) ?: throw ArithmeticError.variable(symbol)

    override fun toDescribe(isTight: Boolean, variables: Map<String, Number>?): String {
        return variables.getValue(symbol)?.toString() ?: symbol
    }

    override fun toString() = toDescribe(true)

    companion object {
        fun of(s: String) = Variables(listOf(Variable1(s)))
    }
}

private fun Map<String, Number>?.getValue(symbol: String): Number? {
    return this?.get(symbol) ?: this?.get("`$symbol`")
}